<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui/css/H-ui.min.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/css/H-ui.admin.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/lib/Hui-iconfont/1.0.8/iconfont.css" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/skin/default/skin.css" id="skin" />
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/css/style.css" />
<link href="${pageContext.request.contextPath}/page/admin/lib/webuploader/0.1.5/webuploader.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page-container">
	<form enctype="multipart/form-data" action="" method="post" class="form form-horizontal" id="form-article-add">
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>姓名：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" class="input-text" value="" placeholder="" id="studentName" name="" autocomplete="off">
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>专业：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<span class="select-box">
					<select id="majorSelect" class="select">
						<option value="0">请选择专业</option>
							<c:forEach items="${majorList}" var="major" varStatus="status">
								<option value="${status.count}">${major.majorName}</option>
							</c:forEach>
					</select>
				</span> 
			</div>
		</div>
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2"><span class="c-red">*</span>班级：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<span class="select-box">
					<select id="classesSelect" class="select">
						<option value="0">请选择班级</option>
					
					</select>
				</span> 
			</div>
		</div>
		
		<div class="row cl">
			<label class="form-label col-xs-4 col-sm-2">密码：</label>
			<div class="formControls col-xs-8 col-sm-9">
				<input type="text" name="" id="password" placeholder="" value="" class="input-text" style="width:100%" autocomplete="off">
				</div>
		</div>
		<div class="row cl">
			<div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-2">
				<button onClick="save();" class="btn btn-primary radius" type="button"><i class="Hui-iconfont">&#xe632;</i> 保存</button>
				<button onClick="layer_close();" class="btn btn-default radius" type="button">&nbsp;&nbsp;取消&nbsp;&nbsp;</button>
			</div>
		</div>
	</form>
</div>

<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery/1.9.1/jquery.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/layer/2.4/layer.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui/js/H-ui.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/static/h-ui.admin/js/H-ui.admin.js"></script> <!--/_footer 作为公共模版分离出去-->
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/My97DatePicker/4.8/WdatePicker.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/jquery.validate.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/validate-methods.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/jquery.validation/1.14.0/messages_zh.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/webuploader/0.1.5/webuploader.min.js"></script> 
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/ueditor/1.4.3/ueditor.config.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/ueditor/1.4.3/ueditor.all.min.js"> </script>
<script type="text/javascript" src="${pageContext.request.contextPath}/page/admin/lib/ueditor/1.4.3/lang/zh-cn/zh-cn.js"></script>
<script id="editor" type="text/plain" style="width: 750px; height: 350px; margin: 0px auto;"></script>
<script type="text/javascript">
$(document).ready(function(){
	 majorSelect();
});

function validate(){//验证输入的准确性
	var res = true;
	var studentName = $("#studentName").val();
	var password = $("#password").val();
	var majorName = $("#majorSelect option:selected").val();
	var classesName = $("#classesSelect option:selected").val();

	if(password==''){
		layer.msg('请输入密码',{icon:0,time:2000});
		res = false;
	}
	if(classesName==0){
		layer.msg('请选择班级',{icon:0,time:2000});
		res = false;
	}
	if(majorName==0){
		layer.msg('请选择专业',{icon:0,time:2000});
		res = false;
	}
	if(studentName==''){
		layer.msg('请输入学生姓名',{icon:0,time:2000});
		res = false;
	}
	return res;
}
function save(){
	var studentName = $("#studentName").val();
	var password = $("#password").val();
	var majorName = $("#majorSelect option:selected").text();
	var classesName = $("#classesSelect option:selected").text();
	if(!validate()){
		
	} else {
		$.ajax({
			type:"get",
			url:"${pageContext.request.contextPath}/StudentManage/addStudent",
			dataType:"json",
			async:false,
			data:{"studentName":studentName,"password":password,"majorName":majorName,"classesName":classesName},
			success:function(data){
				if(data.res==1){
					var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
					parent.layer.close(index); //再执行关闭
					location.reload();
					layer.msg('添加成功',{icon:5,time:2000});
				} else {
					
				}
			},
			error:function(){
				alert("error");	
			}
		});
	}
	
}
function majorSelect(){ //选择专业
	//专业下拉框的内容一开始就被forEach填充了
	
	$("#majorSelect").change(function(){
		if("0"==$("#majorSelect").val()){
			$("#classesSelect").html('');
			$("#classesSelect").append('<option value="0">请选择班级</option>');
		} else {
			var majorName = $("#majorSelect option:selected").text();
			//alert(majorName);
			//去后台读取该专业下的班级
			$.ajax({
				url:"${pageContext.request.contextPath}/StudentManage/getClassesByMajorName",
				type:"get",
				dataType:"json",
				data:{"majorName":majorName},
				async:false,
				success:function(data){
					//循环遍历填充班级下拉框
					$("#classesSelect").html('');
					$("#classesSelect").append('<option value="0">请选择班级</option>');
					$.each(data,function(index,classes){
						$("#classesSelect").append(
							'<option value='+(index+1)+'>'+classes.classesName+'</option>'
						);
					});
					$("#classesSelect").val("1");
				},
				error:function(){
					alert('error');
				}
			}); 		
		}	
	});
	
}

</script>
</body>
</html>